package org.shean.app.rssreader.services;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.util.Log;

public class RSSRefreshReceiver extends BroadcastReceiver {
	
	public static final String PREFS_NAME = "RSSPrefsFile";
	public static final String ALARM_SERVICE = "alarm";

	@Override
	public void onReceive(Context context, Intent intent) {
		SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
        int value = settings.getInt("auto_refresh_time", 0);
        
        Log.d("RSSREFRESH","!!!!!!!!!!!!!!!!!!!!!!!!!!!!! value:" + value);
        
        if(value > 0)
        {
        	PendingIntent mAlarmSender = PendingIntent.getService(context,
	                0, new Intent(context, RSSRefreshService.class), 0);
			
			AlarmManager am = (AlarmManager)context.getSystemService(ALARM_SERVICE);
            am.cancel(mAlarmSender);
            
            long firstTime = SystemClock.elapsedRealtime() + value * 60* 1000;
        	am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    firstTime, value * 60* 1000, mAlarmSender);
        }
	}
}
